﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace GameNesSL.Controls
{
    public delegate void KeySelectedDelegate(JoyButton button, Key key);

    public partial class keyboardbutton : UserControl
    {
        public event KeySelectedDelegate KeySelected = null;

        private void InvokeKeySelected(Key key)
        {
            KeySelectedDelegate selected = KeySelected;
            if (selected != null)
            {
                selected(JoyButton, key);
            }
        }

        public keyboardbutton()
        {
            InitializeComponent();
        }

        public static DependencyProperty KeySetProperty = DependencyProperty.Register("KeySet", typeof(Key), typeof(keyboardbutton), new PropertyMetadata(KeySetChanged));

        private static void KeySetChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((keyboardbutton) d).Key.Text = e.NewValue.ToString();
        }

        public Key KeySet
        {
            get
            {
                return (Key)GetValue(KeySetProperty);
            }
            set
            {
                SetValue(KeySetProperty, value);
            }
        }



        public static DependencyProperty JoyButtonProperty = DependencyProperty.Register("JoyButton",typeof(JoyButton), typeof(keyboardbutton), new PropertyMetadata(JoyButtonChanged));

        private static void JoyButtonChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((keyboardbutton)d).Keydesc.Text = e.NewValue.ToString();
        }

        public JoyButton JoyButton
        {
            get
            {
                return (JoyButton) GetValue(JoyButtonProperty);
            }
            set
            {
                SetValue(JoyButtonProperty, value);
            }
        }



        private bool isSelecting = false;

        private void buttonBorder_MouseEnter(object sender, MouseEventArgs e)
        {
            if (!isSelecting)
            {
                buttonBorder.Background = new SolidColorBrush(Colors.Red);
                Key.Foreground = new SolidColorBrush(Colors.White);
            }
        }

        private void buttonBorder_MouseLeave(object sender, MouseEventArgs e)
        {
            if (!isSelecting)
            {
                buttonBorder.Background = new SolidColorBrush(Colors.LightGray);
                Key.Foreground = new SolidColorBrush(Colors.Red);
            }
        }

        private void buttonBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            isSelecting = true;
            buttonBorder.BorderBrush = new SolidColorBrush(Colors.Red);
            buttonBorder.Background = new SolidColorBrush(Colors.Black);
            Key.Foreground = new SolidColorBrush(Colors.Orange);
            Key.Text = "???";
            InitKeyHandlers();
        }

        private FrameworkElement root = null;

        private void InitKeyHandlers()
        {


            DependencyObject parent = Parent;
            do
            {
                root = parent as FrameworkElement;
                if (root != null)
                {
                    if (root.Parent == null)
                    {
                        root.KeyUp += KeySetEvt;
                        break;
                    }

                    parent = root.Parent;
                }
            }
            while (true);
        }

        private void KeySetEvt(object sender, KeyEventArgs e)
        {
            root.KeyUp -= KeySetEvt;
            isSelecting = false;
            buttonBorder.Background = new SolidColorBrush(Colors.LightGray);
            Key.Foreground = new SolidColorBrush(Colors.Red);
            buttonBorder.BorderBrush = new SolidColorBrush(Colors.Gray);
            Key.Text = e.Key.ToString();
            InvokeKeySelected(e.Key);
        }
    }

    public enum JoyButton
    {
        None,
        B,
        A,
        Left,
        Right,
        Up,
        Down,
        Select,
        Start,
        Load,
        Save

    }
}
